import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {Modal} from 'antd';
import SearchSelect from 'Shared/components/SearchSelect';
import {formatMessage} from '../localize';
import SelectPanel from './SelectPanel';

const EMPTY_ARRAY = [];

// const labelMap = item => this.props.isLabelMap ? `(${item.code}) ${item.name}` : item.name;

class DutyUnitSelectEdit extends PureComponent {
    state = {showModal: false};
    labelMap = item => this.props.isLabelMap ? `(${item.code}) ${item.name}` : item.name;
    labelMapCountry = item => item.chineseName;
    onSearch = filter => this.props.getDutyUnitSimple(Object.assign({filter}, (this.props.type === 2 ? this.props.typeConditions : this.props.conditions)))
        .then(result => result.ok ? result.data.content : EMPTY_ARRAY);

    onClickSearchBtn = () => this.setState({showModal: true});

    onClose = () => this.setState({showModal: false});

    onSelect = record => {
        this.setState({showModal: false});
        this.props.onSelect(record.id ? record : {}, this.props.id);
    };

    render() {
        return [
            <SearchSelect
                key="select"
                allowClear={this.props.allowClear}
                searchOnFocus={true}
                value={this.props.value}
                placeholder={formatMessage({
                    id: 'dutyUnit.placeholder',
                    defaultMessage: '请输入编号或名称查询'
                })}
                keyIndex="id"
                labelMap={this.props.isCountry ? this.labelMapCountry : this.labelMap}
                onSelect={this.onSelect}
                onSearch={this.onSearch}
                onClickSearchBtn={this.onClickSearchBtn}/>,
            <Modal key="modal" visible={this.state.showModal} title={formatMessage({
                id: 'dutyUnit.modal.title',
                defaultMessage: '选择'
            }) + this.props.titleName} width={1000} maskClosable={false} footer={null} destroyOnClose={true} onCancel={this.onClose}>
                <SelectPanel
                    onSelect={this.onSelect}
                    getDutyUnits={this.props.getDutyUnits}
                    isCountry={this.props.isCountry}
                    conditions={this.props.type === 2 ? this.props.typeConditions : this.props.conditions} />
            </Modal>
        ];
    }
}


import * as api from './api';
const EMPTY_OBJECT = {};
const TYPE_OBJECT = {type: 2};
DutyUnitSelectEdit.defaultProps = {
    allowClear: true,
    searchOnFocus: true,
    getDutyUnitSimple: api.getDutyUnitSimple,
    getDutyUnit: api.getDutyUnits,
    conditions: EMPTY_OBJECT,
    typeConditions: TYPE_OBJECT,
    titleName: formatMessage({
        id: 'dutyUnit.titleName',
        defaultMessage: '责任单位'
    }),
    isLabelMap: true,
    isCountry: false
};

DutyUnitSelectEdit.propTypes = {
    /**
     * 选中后回调，(record, props.id)
     */
    onSelect: PropTypes.func.isRequired,
    allowClear: PropTypes.bool,
    /**
     * 增加默认查询条件
     */
    conditions: PropTypes.object,
    getDutyUnitSimple: PropTypes.func,
    getDutyUnits: PropTypes.func,
    id: PropTypes.string,
    /**
     * 查询查询国家
     */
    isCountry: PropTypes.bool,
    /**
     * 查询责任单位API，为空取默认API
     */
    isLabelMap: PropTypes.bool,
    //是否显示code/name
    titleName: PropTypes.string,
    //节点标题
    type: PropTypes.number,
    typeConditions: PropTypes.object,
    value: PropTypes.shape({
        id: PropTypes.string,
        code: PropTypes.string,
        name: PropTypes.string,
    })
};

export default DutyUnitSelectEdit;
